﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YT.DB;
using YT.Commons; 

namespace YT.XH.MF
{
    public partial class frmMDBBackUpMtn : Form
    {
        public frmMDBBackUpMtn()
        {
            InitializeComponent();
        }
        private string mstrFieldName = string.Empty;
        private void btnPath_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();            
            mstrFieldName = saveFileDialog1.FileName.ToString();
            txtFieldName.Text = mstrFieldName;
            
        }


        

        private bool AutoBackUpDB(DBAccess dbcon, string strPath)
        {
            if (MessageBox.Show("您想现在开始备份数据库吗?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {

                    prgBar.Visible = true;

                    Application.DoEvents();                    
                    if (strPath == "") return true;
                    //if (!System.IO.Directory.Exists(strPath))
                    //{
                    //    MessageBox.Show("The path does not exist.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    //    return false;
                    //}

                    //database. when doing back up, the back up name is like Matheus-2009-11-30-ME 
                    string strDBName = dbcon.Con.Database;                    
                    //strPath = strPath +"_"+ strDBName + "-" + DateTime.Now.Date.Year.ToString() + "-" + BaseFunctions.FormatWithZero(DateTime.Now.Date.Month.ToString(), 2) + "-" + BaseFunctions.FormatWithZero(DateTime.Now.Date.Day.ToString(), 2) + ".bak";
                    
                    string strSQL = " backup database [" + strDBName + "] to Disk='" + strPath + "' With init";
                    dbcon.ExecuteSql(strSQL);
                    prgBar.Visible = false;
                    Application.DoEvents();
                    MessageBox.Show("备份完成。", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    prgBar.Visible = false;
                    return false;
                }
                return true;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DBAccess dbcon = new DBAccess(false, false);
            if ( mstrFieldName.StartsWith("C:\\"))
            {
                MessageBox.Show("不能把数据库备份到C:\\", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            AutoBackUpDB(dbcon, mstrFieldName);
            dbcon.CloseCon();
           
        }

        private void frmMDBBackUpMtn_Load(object sender, EventArgs e)
        {
            this.Text = "数据备份";
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
